var lottery = {
    index: 0, //当前转动到哪个位置
    count: 0, //总共有多少个位置
    timer: 0, //setTimeout的ID，用clearTimeout清除
    speed: 200, //初始转动速度
    times: 0, //转动次数
    cycle: 50, //转动基本次数：即至少需要转动多少次再进入抽奖环节
    prize: -1, //中奖位置
    init: function (id) {
        if ($("#" + id).find(".lottery-unit").length > 0) {
            $lottery = $("#" + id);
            $units = $lottery.find(".lottery-unit");
            this.obj = $lottery;
            this.count = $units.length;
            $lottery.find(".lottery-unit-" + this.index).addClass("active");
        };
    },
    roll: function () {
        var index = this.index;
        var count = this.count;
        var lottery = this.obj;
        $(lottery).find(".lottery-unit-" + index).removeClass("active");
        index += 1;
        if (index > count - 1) {
            index = 0;
        };
        $(lottery).find(".lottery-unit-" + index).addClass("active");
        this.index = index;
        return false;
    }
};

function roll(data, fn) {
    //console.log(123)
    lottery.times += 1;
    lottery.roll();
    if (lottery.times > lottery.cycle + 10 && lottery.prize == lottery.index) {
        clearTimeout(lottery.timer);
        lottery.prize = -1;
        lottery.times = 0;
        click = false;
        fn && fn();
    } else {
        if (lottery.times < lottery.cycle) {
            lottery.speed -= 10;
        } else if (lottery.times == lottery.cycle) {
            lottery.prize;

            // var index = Math.random()*(lottery.count)|0;
            // lottery.prize = index;
        } else {
            if (lottery.times > lottery.cycle + 10 && ((lottery.prize == 0 && lottery.index == 7) || lottery.prize == lottery.index + 1)) {
                lottery.speed += 110;
            } else {
                lottery.speed += 20;
            }
        }
        if (lottery.speed < 40) {
            lottery.speed = 40;
        };
        //console.log(lottery.times+'^^^^^^'+lottery.speed+'^^^^^^^'+lottery.prize);
        lottery.timer = setTimeout(function () {
            roll(data, fn);
        }, lottery.speed);
    }
    return false;
}

var click = false; //是否已进入转动抽奖
var data;

var commit = {
    url: 'https://www.fastmock.site/mock/8eca981992ab7c97a8cbaec2294a9000/nine/',
    pages: 1,
    islottery: false,
    isStart: false,
    isEnd: false,
    isClick: false,
    hxCode:'',
    tips: function (str, ico, fn, t) {
        if (!t) {
            t = 1500;
        }
        new hdpop({
            alert: str,
            alert_time: t,
            dw: 'px',
            closeBack: function () {
                fn && fn();
            }

        })
    },

    lottery: function () {
        $('#lottery .pointer').off('click');
        $('#lottery .pointer').on('click', function (e) {
            // e.preventDefault();
            if (!commit.isStart) {
                commit.isStart = true;
                if (!commit.islottery) {
                    commit.islottery = true;
                    $.ajax({
                        url: commit.url + 'lottery.jsp',
                        type: 'POST',
                        xhrFields: { withCredentials: true },
                        data: {
                        },
                        success: function (data) {
                            // console.log(data)
                            if (data.code == 1) {
                                data.hxCode && (commit.hxCode=data.hxCode)

                                switch (+data.awardType) {
                                    case 1:
                                        commit.lotteryInit(0, '恭喜您抽中'+'智能电热水壶'+'！核销码是：'+data.hxCode)
                                        break;
                                    case 2:
                                        commit.lotteryInit(1, '恭喜您抽中'+'家用电炒锅'+'！核销码是：'+data.hxCode)
                                        break;
                                    case 3:
                                        commit.lotteryInit(2, '恭喜您抽中'+'车载吸尘器'+'！核销码是：'+data.hxCode)
                                        break;
                                    case 4:
                                        commit.lotteryInit(3, '恭喜您抽中'+'美的微波炉'+'！核销码是：'+data.hxCode)
                                        break;
                                    case 5:
                                        commit.lotteryInit(4, '恭喜您抽中'+'苹果12'+'！核销码是：'+data.hxCode)
                                        break;
                                    case 6:
                                        commit.lotteryInit(5, '恭喜您抽中'+'苹果ipad'+'！核销码是：'+data.hxCode)
                                        break;
                                    case 7:
                                        commit.lotteryInit(7, '恭喜您抽中'+'半球电磁炉'+'！核销码是：'+data.hxCode)
                                        break;
                                    default:
                                        commit.lotteryInit(6, '谢谢参与')
                                        break;
                                }

                            }else if(data.code==2){
                                ommit.lotteryInit(6, '谢谢参与')
                            }else {
                                commit.tips(data.msg)
                                if (data.code == -3) {
                                    commit.tips('请先填写报名信息', 1200, function () {
                                        $('html,body').animate({ 'scrollTop': '266px' }, 500);
                                    });
                                }
                                commit.isStart = false;
                            }

                        },
                        error: function () {
                            alert('网络不佳，请稍后重试')
                            commit.isStart = false;
                        }
                    })
                } else {
                    commit.hxCode?alert('您已抽过奖了，核销码是：'+commit.hxCode):commit.tips('您已抽过奖了')
                    commit.isStart = false;
                }
            } else { }
        })
    },
    lotteryInit: function (prizeNum, tip) {
        if (click) {
            return false;
        } else {
            lottery.speed = 100;
            lottery.prize = prizeNum;
            roll(data, function () {
                setTimeout(function () {
                    alert(tip)
                    commit.isStart = false;
                }, 1000);
            });
            click = true;
            return false;
        }
    },

}
window.onload = function () {
    commit.lottery()
    lottery.init('lottery');
}

